package com.plm.k8s.myk8sapi.cmd.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.plm.k8s.myk8sapi.cmd.AbstractSecretCmLocalCmd;
import com.plm.k8s.myk8sapi.cmd.IConfigMapCmd;
import com.plm.k8s.myk8sapi.pojo.entity.configmap.ApiConfigMapEntity;
import com.plm.k8s.myk8sapi.pojo.entity.configmap.ConfigMapEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author 潘磊明
 * 版本号：1.0
 * 创建日期：2021/8/4 16:44
 */
@Service
public class ConfigMapCmdImpl extends AbstractSecretCmLocalCmd implements IConfigMapCmd {

    @Override
    public List<ConfigMapEntity> listConfigMap(boolean isAll, String namespace, String nameFilter, Map<String, String> labelFilter) throws Exception {
        String cmd = generateListConfigMapCmd(isAll, namespace, nameFilter, labelFilter);
        ApiConfigMapEntity apiConfigMapEntity = execForYamlObject(cmd, ApiConfigMapEntity.class);
        if (apiConfigMapEntity == null) return Collections.emptyList();
        return apiConfigMapEntity.getItems();
    }

    @Override
    public boolean createConfigMap(String name, Map<String, String> keyValue, List<File> files, String namespace) throws Exception {
        if (!StringUtils.hasText(name)) throw new Exception("ConfigMap名称不能为空");
        if (CollectionUtil.isEmpty(keyValue) && CollectionUtil.isEmpty(files)) throw new Exception("ConfigMap的键值对或文件列表不能全为空");
        String cmd = generateCreateConfigMapCmd(name, keyValue, files, namespace);
        String result = exec(cmd);
        if (!String.format("configmap/%s created", name).equals(trimBr(result))) {
            throw new Exception(result);
        }
        return true;
    }

    @Override
    public boolean deleteConfigMap(String name, String namespace) throws Exception {
        if (!StringUtils.hasText(name)) throw new Exception("ConfigMap名称不能为空");
        String cmd = generateDeleteConfigMapCmd(name, namespace);
        String result = exec(cmd);
        if (!String.format("configmap \"%s\" deleted", name).equals(trimBr(result))) {
            throw new Exception(result);
        }
        return true;
    }

    private String generateDeleteConfigMapCmd(String name, String namespace) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("delete cm ").append(name);
        if (StringUtils.hasText(namespace)) addNamespace(sb, namespace);
        return sb.toString();
    }

    private String generateCreateConfigMapCmd(String name, Map<String, String> keyValue, List<File> files, String namespace) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("create cm ").append(name);
        if (CollectionUtil.isNotEmpty(keyValue)) addKeyValue(sb, keyValue);
        if (CollectionUtil.isNotEmpty(files)) addFile(sb, files);
        if (StringUtils.hasText(namespace)) addNamespace(sb, namespace);
        return sb.toString();
    }

    private String generateListConfigMapCmd(boolean isAll, String namespace, String nameFilter, Map<String, String> labelFilter) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("get cm -o yaml");
        if (isAll) {
            sb.append(" -A");
        } else {
            if (StringUtils.hasText(namespace)) addNamespace(sb, namespace);
        }
        if (StringUtils.hasText(nameFilter)) addNameFilter(sb, nameFilter);
        if (CollectionUtil.isNotEmpty(labelFilter)) addLabelFilter(sb, labelFilter);
        return sb.toString();
    }
}
